<?php
/**
 * Created by PhpStorm.
 * User: lcz
 * Date: 2018/4/8
 * Time: 14:09
 * 分类
 */
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\GoodsCategory;
use think\Db;

class Category extends Api{
	protected $noNeedRight = ['*'];
	protected $noNeedLogin = ['*'];
	
	/**
	 * 获取顶级
	 */
	public function index(){
		$model = new GoodsCategory();
		
		$data['category'] = $model->getListById(0);
		
		$time = time();
		$where = [
			'ai.position' => 4,
			'ai.begin_time' => ['elt', $time],
			'ai.end_time' => ['egt', $time],
		];
		$data['banners'] = Db::name('appIndex')->alias('ai')
			->join('appClass ac', 'ai.app_class_id = ac.id', 'left')
			->where($where)
			->field('ai.position, ai.app_image, ai.name, ai.url, ai.param_id, ai.login, ac.ios_class, ac.android_class')
			->order(['ai.sort' => 'ASC'])
			->select();
		
		$this->success('查询成功', $data);
	}
	
	/**
	 * 获取下级分类
	 */
	public function getSonCategory(){
		$id = input('post.id');
		if(!$id) {
			$this->error('参数错误');
		}
		
		$model = new GoodsCategory();
		$sons = $model->getListById($id);
		$returnData = [];
		if($sons) {
			foreach($sons as $son){
				$son['son_category'] = $model->getListById($son['id']);
				$returnData[] = $son;
			}
		}
		$this->success('查询成功', $returnData);
	}
	
	
	/**
	 * 获取商品分类列表
	 */
	public function getCategoryGoods(){
		$cid = input('post.id');
		!$cid && $this->error('分类ID不能为空');
		
		$page = input('post.page', 1, 'int');
		
		$goods = Db::name('goods')
			->where([
				'category_id' => $cid,
				'is_on_sale' => \app\common\model\Goods::IS_ON_SALE_Y,
				'effective_time' => ['elt', time()]
			])
			->field('id, goods_name, original_image, shop_price, market_price, exchange_integral, exchange_integral, sale_number, use_point as point, give_balance as sendmoney')
			->page($page)
			->select();
		$this->success('查询成功', $goods);
	}
	
	
}
